package com.guan.study.poi.write;


import com.guan.study.poi.UserVo;
import com.guan.study.poi.UseCaseExportVo;
import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2023/7/1 1:27
 * @Author: yaobai
 * @Description:
 */
public class Write03 {

    private static final String flieName = "E:\\WorkDirTest2\\"+System.currentTimeMillis()+"导出用户.xlsx";

    public static void main(String[] args) throws Exception {

        exportExcel02(data());

        System.out.println("Export complete.");
    }


    private static void exportExcel02(List<UserVo> vos) throws Exception {

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        for (int i = 0; i < vos.size(); i++) {
            Row row = sheet.createRow(i);
            UserVo vo = vos.get(i);

            Cell cell0 = row.createCell(0);
            cell0.setCellValue(vo.getName());

            Cell cell1 = row.createCell(1);
            cell1.setCellValue(vo.getUid());

            Cell cell2 = row.createCell(2);
            cell2.setCellValue(vo.getAge());

            URL imageUrl = new URL(vo.getHeadUrl());
            byte[] imageData = IOUtils.toByteArray(imageUrl);
            Cell cell = row.createCell(3);
            addImageToCell(workbook, sheet, cell, imageData);

        }

        workbook.write(new FileOutputStream(flieName));
        System.out.println("Excel 文件已生成。");

    }

    private static List<UserVo> data()  {

        List<UserVo> userVos = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            UserVo vo = new UserVo();
            vo.setId("100"+i);
            vo.setUid("ZhangSan_"+i);
            vo.setName("张三"+i);
            vo.setAge(10+i);
            vo.setHeadUrl("http://www.baidu.com/img/bdlogo.png");

            userVos.add(vo);
        }

        return userVos;
    }


    private static void addImageToCell(Workbook workbook, Sheet sheet, Cell cell, byte[] imageData) {
        // 将字节数组转换为图片格式
        int pictureIndex = workbook.addPicture(imageData, Workbook.PICTURE_TYPE_JPEG);

        // 创建一个绘图对象
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        CreationHelper helper = workbook.getCreationHelper();

        // 创建锚点并设置图片位置
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(cell.getColumnIndex());
        anchor.setRow1(cell.getRowIndex());
        anchor.setCol2(cell.getColumnIndex() + 1);
        anchor.setRow2(cell.getRowIndex() + 1);

        //偏移量 1cm = 360000
        anchor.setDx1(18000);
        anchor.setDy1(18000);
        anchor.setDx2(18000);
        anchor.setDy2(18000);

        // 插入图片
        Picture picture = drawing.createPicture(anchor, pictureIndex);
        //缩放
        picture.resize(0.9);
    }
}
